//
//  DefaultTableViewCell.swift
//  DaGlass
//
//  Created by 李然 on 2023/2/16.
//

import UIKit
import MoyoungLib
import RxOptional

open class DefaultTableViewCell: TableViewCell {
    
    lazy var leftImageView: ImageView = {
        let view = ImageView(frame: CGRect())
        view.contentMode = .scaleAspectFit
        return view
    }()
    
    lazy var titleLabel: Label = {
        let view = Label()
        view.font = Fonts.mediumSystemFont(ofSize: 14)
        view.textColor = UIColor.hex(str: "222222")
        return view
    }()

    lazy var subTitleLabel: Label = {
        let view = Label()
        view.numberOfLines = 0
        return view
    }()
    
    lazy var rightImageView: ImageView = {
        let view = ImageView(frame: CGRect())
//        view.image = R.image.ic_continue()
        return view
    }()
    
    lazy var detailLabel: Label = {
        let view = Label()
        view.font = Fonts.systemFont(ofSize: 12)
        view.textColor = UIColor.hex(str: "222222")
        return view
    }()
        
    lazy var switchView: Switch = {
        let view = Switch()
        view.isHidden = true
        return view
    }()
    
    open override func makeUI() {
        super.makeUI()
        
        stackView.spacing = 4
        
        let titleStackView = StackView(arrangedSubviews: [self.titleLabel, self.subTitleLabel])
        titleStackView.axis = .vertical
        titleStackView.spacing = 2
        
        stackView.addArrangedSubview(leftImageView)
        stackView.addArrangedSubview(titleStackView)
        
        let spaceLabel = Label()
        spaceLabel.text = " "
        spaceLabel.setPriority(.fittingSizeLevel, for: .horizontal)
        stackView.addArrangedSubview(spaceLabel)
        stackView.addArrangedSubview(detailLabel)
        stackView.addArrangedSubview(switchView)
        switchView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 52, height: 32))
        }
        stackView.addArrangedSubview(rightImageView)
    }
    
    open func bind(to viewModel: DefaultTableViewCellViewModel) {
        
        viewModel.icon.asDriver().filterNil().drive(leftImageView.rx.image).disposed(by: rx.disposeBag)
        viewModel.icon.asDriver().map { $0 == nil }.drive(leftImageView.rx.isHidden).disposed(by: rx.disposeBag)
        
        viewModel.title.asDriver().drive(titleLabel.rx.text).disposed(by: rx.disposeBag)
        viewModel.title.asDriver().replaceNilWith("").map { $0.isEmpty }.drive(titleLabel.rx.isHidden).disposed(by: rx.disposeBag)
        
        viewModel.subTitle.asDriver().drive(subTitleLabel.rx.text).disposed(by: rx.disposeBag)
        viewModel.subTitle.asDriver().replaceNilWith("").map { $0.isEmpty }.drive(subTitleLabel.rx.isHidden).disposed(by: rx.disposeBag)
        
        viewModel.detail.asDriver().drive(detailLabel.rx.text).disposed(by: rx.disposeBag)
        viewModel.detail.asDriver().replaceNilWith("").map { $0.isEmpty }.drive(detailLabel.rx.isHidden).disposed(by: rx.disposeBag)
        
        viewModel.switchOn.asDriver().drive(switchView.rx.isOn).disposed(by: rx.disposeBag)
        switchView.rx.isOn.bind(to: viewModel.switchChanged).disposed(by: rx.disposeBag)
        viewModel.switchEnable.asObservable().bind(to: switchView.rx.isEnabled).disposed(by: rx.disposeBag)
    }
}
